﻿#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2009, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Streambolics.Hp41s
{
    /// <summary>
    ///     The object that extracts individual tokens from the source
    ///     code.
    /// </summary>
    /// <remarks><para>
    ///     The HP-41S tokenizer expects to be applied on a single line
    ///     of code. When compiling a program, instanciate a Tokenizer
    ///     for each line in turn.
    /// </para></remarks>

    public class Tokenizer
    {
        private Scanner _Scanner;
        private string _Current;

        public Tokenizer (string aString)
        {
            _Scanner = new Scanner (aString);
            _Scanner.SkipWhiteSpace ();
            if (_Scanner.Index < 2)
            {
                Next ();
            }
            Next ();
        }

        /// <summary>
        ///     Moves to the next token. 
        /// </summary>
        /// <remarks><para>
        ///     It is legal to call Next() even if you already
        ///     are after the last token on the line.
        /// </para></remarks>

        public void Next ()
        {
            _Scanner.SkipWhiteSpace ();
            if (_Scanner.More)
            {
                StringBuilder sb = new StringBuilder ();
                if (_Scanner.Current == '"')
                {
                    sb.Append ('"');
                    _Scanner.Next ();
                    while (_Scanner.More && _Scanner.Current != '"')
                    {
                        sb.Append (_Scanner.Current);
                        _Scanner.Next ();
                    }
                    sb.Append ('"');
                    _Scanner.Next ();
                }
                else
                {
                    while (_Scanner.More && _Scanner.Current != ' ' && _Scanner.Current != '\t')
                    {
                        sb.Append (_Scanner.Current);
                        _Scanner.Next ();
                    }
                }
                _Current = sb.ToString ();
            }
            else
            {
                _Current = null;
            }
        }

        /// <summary>
        ///     The current token.
        /// </summary>

        public string Current
        {
            get
            {
                return _Current;
            }
        }

        /// <summary>
        ///     Whether there are more tokens, or you are at the
        ///     end of the line.
        /// </summary>

        public bool More
        {
            get
            {
                return _Current != null;
            }
        }
    }
}
